package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.*;
import view.*;

public class GameController {
	private ViewFrame frame;
	private Blackjack blackjack;
	private GameView game;
	private File fileio;
	private MenuBar menu;

	public GameController(ViewFrame frame_, Blackjack blackjackModel_) {
		frame = frame_;
		blackjack = blackjackModel_;
		fileio=new File("savedGame.dat");
		
		menu = new MenuBar(frame);
		
		//menu.setMenuBetListener(new betListener());//actionlistener missing
		menu.setMenuHitListener(new hitListener());
		menu.setMenuLoadListener(new loadListener());
		menu.setMenuStandListener(new standListener());
	//	menu.setMenuSurrenderListener(new surrenderListener());
		frame.setViewMenuBar(menu);
		
		
		
		StartView.addPlayerButton.addActionListener(new addPlayerListener());
		StartView.start.addActionListener(new startGameListener());
		
		//frame.getBasePanel().addButtonListener(new buttonListener()); 

	}
	
	class startGameListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			game = new GameView(1, blackjack.getPlayerAvatars());
			
			blackjack.addObserver(game); //Adds the view as an observer of the model
			
			ViewFrame.setBasePanel(game);
			
			game.addHitListener(new hitListener());
			game.addStandListener(new standListener());
			
			blackjack.InitGame();
			
			blackjack.dealCards(); //Detta ska h�nda efter att man placerat sina bets
			
			game.setNumberOfCards(0, blackjack.getPlayerHand(0).getNoOfCards());
			
			game.drawHand(0, blackjack.getPlayerHand(0)); //Draw the hand of the first player
			game.drawHand(1, blackjack.getDealerHand());
			
			game.setActivePlayer(0); //First player has index 0
			
		
		}
		
	}
	
	class addPlayerListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			blackjack.addPlayer(StartView.getPlayerName(), StartView.getSelectedAvatar(), 0);
			
		}

	}

	class hitListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			blackjack.PlayerHit(game.getActivePlayer());
			
			game.Hit(blackjack.getPlayerHand(game.getActivePlayer()));
			
			blackjack.checkPoints(game.getActivePlayer());
			
		}

	}
	
	class standListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			blackjack.Stand();
			
			game.updateDealerHand(blackjack.getDealerHand());
			
		}

	}
	
	class newgameListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//Main main =new Main();
			//start new gaem
		}
		
	}
class loadListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		blackjack=new Blackjack(fileio.LoadFile());
	}
	
}

class saveListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		fileio.SaveFile(blackjack);
	}
	
}


}
